import { Component, OnInit, Injector } from '@angular/core';
import { CityListComponent } from '../../school/cityList';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { HttpClient } from "@angular/common/http";
import { Router, ActivatedRoute, Params } from '@angular/router';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
@Component({
  selector: 'app-studentadd',
  templateUrl: './studentadd.component.html',
  styleUrls: ['./studentadd.component.scss']
})
export class StudentaddComponent implements OnInit {
  studentid;
  selclassname; // 班级 
  project = []; // 项目
  departmentName; // 系
  username; // 用户名
  userpassword; // 密码
  studentname; // 学生姓名
  studentphone; // 学生电话
  stdentnun; // 学号
  allstudentdata;
  radioValue = 'true';
  aclear; // 延迟搜索
  academeIdata; // 院id
  collegeDepartmentNamedata; // 系名称
  academearr = []; // 系数组
  classarr = []; // 班级数组
  classclear;
  studentuparr = [];

  ngOnInit() {
    this.academeIdata = sessionStorage.getItem('academeId');
    this.collegeDepartmentNamedata = sessionStorage.getItem('collegeDepartmentName');
    const xdata = { "academeId": this.academeIdata, "collegeDepartmentName": this.collegeDepartmentNamedata };
    this.http1.post<any>("School/GetDepartmentByAcademe", xdata).then(e => {
      this.academearr = e;
    });
    this.activatedRoute.queryParams.subscribe(
      data => this.studentid = (data['studentId']));
    if (this.studentid !== "add") {
      this.http1.get<any>(`StudentInfo/GetStudentInfo?input=${this.studentid}`).then(e => {
        this.departmentName = e.collDepartmentId;
        // 通过系查班级
        const classdata = { "cid": this.departmentName, "name": "" };
        this.http1.post<any>("ClassInfo/GetAllClassInfoByDepartment", classdata).then(d => {
          this.classarr = d;
        });
        this.selclassname = e.classInfoId;
        this.username = e.userName;
        this.userpassword = e.userPwd;
        this.studentname = e.personName;
        this.studentphone = e.phoneNumber;
        this.stdentnun = e.personNo;
      });
    }
  }
  constructor(injecter: Injector,
    public http1: HttpService,
    public http: HttpClient,
    private activatedRoute: ActivatedRoute,
    public message: NzMessageService,
    public router: Router
  ) { }


  /**
   * 保存学生信息
   *
   */


  async serve(type: string) {
    this.studentuparr = [];
    const that = this;

    if (this.userpassword === undefined) {
      this.userpassword = "123456";
    }

    const data = {
      "collDepartmentId": this.departmentName, "classInfoId": this.selclassname,
      "userName": this.username, "userPwd": this.userpassword, "personName": this.studentname, "phoneNumber": this.studentphone, "personNo": this.stdentnun
    };
    if (this.studentid !== "add") {
      data["id"] = this.studentid;
    }

    Object.keys(data).forEach(function (trait) {
      that.studentuparr.push(data[trait]);
    });
    let i = 0;
    that.studentuparr.forEach(element => {
      if (element === undefined || element === "") {
      } else {
        i++;
      }
    });
    if (i === that.studentuparr.length) {
      if (this.studentid !== "add") {
        await that.http1.post<any>("StudentInfo/UpdateStudentInfo", data);
      } else {
        await that.http1.post<any>("StudentInfo/CreatStudentInfo", data);
      }
    } else {
      this.message.info('请填写完整');
      return false;
    }
    this.message.create(type, `保存成功`);
    this.router.navigate(['app/studentindex']);

  }


  // 通过系查班级
  async provinceChangesol(edata) {
    this.departmentName = edata;
    this.selclassname = undefined;
    const classdata = { "cid": this.departmentName, "name": "" };
    this.http1.post<any>("ClassInfo/GetAllClassInfoByDepartment", classdata).then(e => {
      this.classarr = e;
    });

  }
  async classChangesol(e) {
    this.selclassname = e;
  }

  // 班级模糊查询
  async classsearchChange(e) {
    const that = this;
    clearTimeout(this.classclear);
    this.aclear = setTimeout(() => {
      that.classlist(e);
    }, 1000);
  }

  classlist(edata) {
    const classdata = { "cid": this.departmentName, "name": edata };
    this.http1.post<any>("ClassInfo/GetAllClassInfoByDepartment", classdata).then(e => {
      this.classarr = e;
    });
  }
  // 系模糊查询
  facultysearchChange(e) {
    const that = this;
    clearTimeout(this.aclear);
    this.aclear = setTimeout(() => {
      that.facultylist(e);
    }, 1000);
  }


  facultylist(x) {
    const xdata = { "academeId": this.academeIdata, "collegeDepartmentName": x };
    this.http1.post<any>("School/GetDepartmentByAcademe", xdata).then(e => {
      this.academearr = e;

    });
  }
}

